<template>
  <div class="p-2">
    <el-row :gutter="20">
      <el-col :sm="24" :xs="24">
        <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
          <div v-show="showSearch" class="mb-[10px]">
            <el-card shadow="hover">
              <el-form v-show="showSearch" ref="queryFormRef" :model="queryParams" :inline="true">
                <el-form-item label="组织名称" label-width="100" prop="flowName">
                  <el-input v-model="queryParams.flowName" placeholder="请输入组织名称" @keyup.enter="handleQuery" />
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                  <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-form>
            </el-card>
          </div>
        </transition>
        <el-card shadow="hover">
          <template #header>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5" style="display: flex; margin-left: auto">
                <el-input v-model="queryParams.flowName" placeholder="请输入组织名称" clearable @keyup.enter="handleQuery"> </el-input>
                <el-tooltip class="item" effect="dark" content="点击查询" placement="top">
                  <el-button style="margin: 0 12px" circle @click="handleQuery()">
                    <el-icon><Search /></el-icon>
                  </el-button>
                </el-tooltip>
              </el-col>
              <right-toolbar v-model:show-search="showSearch" @query-table="handleQuery" style="margin-left: unset"></right-toolbar>
            </el-row>
          </template>
          <div ref="tableContainer">
            <el-table :height="tableHeight" v-loading="loading" border :data="taskList" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column align="center" type="index" label="序号" width="60"></el-table-column>
              <el-table-column :show-overflow-tooltip="true" prop="flowName" align="center" label="组织名称"></el-table-column>
              <el-table-column align="center" prop="flowCode" label="变更单号"></el-table-column>
              <el-table-column align="center" prop="categoryName" label="涉及组织"></el-table-column>
              <el-table-column align="center" prop="nodeName" label="创建人"></el-table-column>
              <el-table-column align="center" prop="nodeName" label="创建时间"></el-table-column>

              <el-table-column align="center" prop="createByName" label="更新时间"></el-table-column>
              <el-table-column align="center" prop="createTime" label="审核状态" width="160"></el-table-column>
              <el-table-column fixed="right" label="操作" width="100">
                <template #default="scope">
                  <el-tooltip content="详情" placement="top">
                    <el-button v-hasPermi="['organ:record:view']" link type="primary" icon="View" @click="handleUpdate(scope.row)" />
                  </el-tooltip>
                </template>
              </el-table-column>
            </el-table>
            <pagination
              v-show="total > 0"
              v-model:page="queryParams.pageNum"
              v-model:limit="queryParams.pageSize"
              :total="total"
              @pagination="handleQuery"
            />
          </div>
        </el-card>
      </el-col>
    </el-row>
    <!-- 添加或修改用户配置对话框 -->
    <el-dialog ref="formDialogRef" v-model="dialog.visible" :title="dialog.title" append-to-body @close="closeDialog" class="p-6090-dialog">
      <div style="font-size: 16px; font-weight: 700; padding-bottom: 15px">基本信息</div>
      <el-form ref="userFormRef" :model="form" label-width="100px" label-suffix=":">
        <el-form-item label="单号" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入单号" maxlength="30" />
        </el-form-item>
        <el-form-item label="变更标题" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入变更标题" maxlength="30" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-form>
      <div style="font-size: 16px; font-weight: 700; padding-bottom: 15px">
        <span style="padding-right: 15px">变更信息</span>
        <el-tag type="primary" size="large">修改</el-tag>
      </div>

      <el-table border :data="moveList">
        <el-table-column align="center" type="index" label="序号" width="60"></el-table-column>
        <el-table-column label="组织名称" align="center" prop="deptId" />
        <el-table-column label="组织编码" align="center" prop="userId" />
        <el-table-column label="变更前" align="center" prop="orderNum" />
        <el-table-column label="变更后" align="center" prop="testKey" />
        <el-table-column label="生效日期" align="center" prop="value" />
        <el-table-column label="操作人信息" align="center" prop="value" />
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel()">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import api from '@/api/system/user';

import { pageByTaskWait } from '@/api/workflow/task';
import { TaskQuery, FlowTaskVO } from '@/api/workflow/task/types';
import workflowCommon from '@/api/workflow/workflowCommon';
import { RouterJumpVo } from '@/api/workflow/workflowCommon/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { wf_business_status } = toRefs<any>(proxy?.useDict('wf_business_status'));
import UserSelect from './headerSelect.vue';
import { ref } from 'vue';
import { useTableHeight } from '@/hooks/useTableHeight';
import { UserForm, UserVO } from '@/api/system/user/types';
import { organVO } from '@/api/organization/archives/types';
import { DeptTreeVO, DeptVO } from '@/api/system/dept/types';
import { title } from 'process';

const userSelectRef = ref<InstanceType<typeof UserSelect>>();
//提交组件
const queryFormRef = ref<ElFormInstance>();
// 遮罩层
const loading = ref(true);
// 选中数组
const ids = ref<Array<any>>([]);
// 非单个禁用
const single = ref(true);
// 非多个禁用
const multiple = ref(true);
// 显示搜索条件
const showSearch = ref(false);
// 总条数
const total = ref(0);
// 模型定义表格数据
const taskList = ref([]);
//申请人id
const selectUserIds = ref<Array<number | string>>([]);
//申请人选择数量
const userSelectCount = ref(0);
// 查询参数
const queryParams = ref<TaskQuery>({
  pageNum: 1,
  pageSize: 10,
  nodeName: undefined,
  flowName: undefined,
  flowCode: undefined,
  createByIds: []
});
// 部门树
const deptName = ref('');
const deptTreeRef = ref<ElTreeInstance>();
const deptOptions = ref<DeptTreeVO[]>([]);
const enabledDeptOptions = ref<DeptTreeVO[]>([]);
//新增弹窗
const formDialogRef = ref<ElDialogInstance>();
const userFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const form: UserForm = {
  userId: undefined,
  deptId: undefined,
  userName: '',
  nickName: undefined,
  password: '',
  phonenumber: undefined,
  email: undefined,
  sex: undefined,
  status: '0',
  remark: '',
  postIds: [],
  roleIds: [],
  value: undefined
};
//自适应表格高度
const tableContainer = ref(null);
const { tableHeight } = useTableHeight({
  containerRef: tableContainer,
  offset: 82
});
const moveList = ref([]);

onMounted(() => {
  getWaitingList();
});
/** 搜索按钮操作 */
const handleQuery = () => {
  getWaitingList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  queryParams.value.pageNum = 1;
  queryParams.value.pageSize = 10;
  queryParams.value.createByIds = [];
  userSelectCount.value = 0;
  selectUserIds.value = [];
  deptTreeRef.value?.setCurrentKey(undefined);

  handleQuery();
};
// 多选框选中数据
const handleSelectionChange = (selection: any) => {
  ids.value = selection.map((item: any) => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
};
//分页
const getWaitingList = () => {
  loading.value = true;
  pageByTaskWait(queryParams.value).then((resp) => {
    taskList.value = resp.rows;
    total.value = resp.total;
    loading.value = false;
  });
};

//新增组织
const handleAdd = async () => {
  reset();
  const { data } = await api.getUser();
  dialog.visible = true;
  dialog.title = '组织档案详情';
  // postOptions.value = data.posts;
  // roleOptions.value = data.roles;
  // form.value.password = initPassword.value.toString();
};

/** 重置操作表单 */
const reset = () => {
  // form.value = { ...from };
  userFormRef.value?.resetFields();
};
/** 取消按钮 */
const cancel = () => {
  dialog.visible = false;
  reset();
};
/** 修改按钮操作 */
const handleUpdate = async (row?: UserForm) => {
  reset();
  const userId = row?.userId || ids.value[0];
  const { data } = await api.getUser(userId);
  dialog.visible = true;
  dialog.title = '组织变更详情';
  //   Object.assign(form.value, data.user);
  //   postOptions.value = data.posts;
  //   roleOptions.value = data.roles;
  //   form.value.postIds = data.postIds;
  //   form.value.roleIds = data.roleIds;
  //   form.value.password = '';
};

/** 提交按钮 */
const submitForm = () => {
  userFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      form.value.userId ? await api.updateUser(form.value) : await api.addUser(form.value);
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getWaitingList();
    }
  });
};
/**
 * 关闭用户弹窗
 */
const closeDialog = () => {
  dialog.visible = false;
  resetForm();
};

/**
 * 重置表单
 */
const resetForm = () => {
  userFormRef.value?.resetFields();
  userFormRef.value?.clearValidate();

  // form.value.id = undefined;
  // form.value.status = '1';
};
</script>
